<?php

namespace App\Admin\Controllers;

use App\Models\Project;
use App\Models\ReservationLog;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ReservationLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ReservationLog(), function (Grid $grid) {
            $grid->model()
                ->with(['projects'])
                ->orderBy("id", "desc");

            $grid->column('id')->sortable();
            $grid->column('date')->display(function () {
                return $this->date?->format('Y-m-d');
            });
            $grid->column('status')->radio(ReservationLog::STATUS_OPTIONS);
            $grid->column('name');
            $grid->column('phone');
            $grid->column('projects', '预约项目')
                ->pluck("title")
                ->label();
            $grid->column('time')->label();
            $grid->column('remark');
//            $grid->column('user_id');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }


    protected function timelineView(Content $content) {
        $resourceOptions = Project::getFullResourceOptions();
        return $content
            ->translation($this->translation())
            ->title("预约看板")
            ->description($this->description()['index'] ?? trans('admin.list'))

            ->body(admin_view("admin::pages.projectTimeLine",[
                'resources' => $resourceOptions,
            ]));
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ReservationLog(), function (Show $show) {
            $show->field('id');
            $show->field('date');
            $show->field('phone');
            $show->field('name');
            $show->field('time');
            $show->field('remark');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(ReservationLog::with(['projects']), function (Form $form) {

            $form->display('id');

            $form->date('date')->options([
                'minDate' => today(),
            ])->required();
            $form->multipleSelect('projects')
                ->options(Project::twoLevelOptions())
                ->customFormat(function ($val) {
                    return collect($val)->pluck('id')->toArray();
                })
                ->load('time', admin_route('admin.api.project.multiple_time_range'))
                ->required();

            $form->tel('phone')->required();
            $form->text('name')->required();
            $form->radio('status')
                ->options(ReservationLog::STATUS_OPTIONS)
                ->default(0)
                ->required();
            $form->select('time')->required();
            $form->text('remark');
            $form->text('user_id')->required();

            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function (Form $form) {
                $model = $form->model();
                $isUpdate = (bool)$model->id;
                $date = $form->input('date');
                $time = $form->input('time');
                $projects = $form->input('projects');
                if ($date && $time && $projects) {
                    $date = Carbon::parse($date);
                    if (!ReservationLog::checkTimeCanUse($projects, $date, $time, [$model->id]))
                        return $form->responseValidationMessages('time', '该时段已不可预约');

                }

            });

        });
    }
}
